using System;

using Microsoft.SPOT;
using Microsoft.SPOT.Input;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using Microsoft.SPOT.Hardware;
using System.Net;
using System.Net.Sockets;
namespace Project
{
    class EditDateWindow : Window
    {
        Canvas canvas = new Canvas();
        private Window editDateWindow;
        int hour = 00;
        int min = 00;
        int day = 01;
        int month = 10;
        int year = 2012;

        Text text_hour;
        Text text_min;
        Text text_day;
        Text text_month;
        Text text_year;
        
        public EditDateWindow(Window mw)
        {
            
            editDateWindow = new Window();
            editDateWindow.Width = SystemMetrics.ScreenWidth;
            editDateWindow.Height = SystemMetrics.ScreenHeight;

            CreateWindow();
        }
        public void CreateWindow()
        {       
            text_hour = new Text(Resources.GetFont(Resources.FontResources.NinaB), hour.ToString());
            text_min = new Text(Resources.GetFont(Resources.FontResources.NinaB), min.ToString());
            text_day = new Text(Resources.GetFont(Resources.FontResources.NinaB), day.ToString());
            text_month = new Text(Resources.GetFont(Resources.FontResources.NinaB), month.ToString());
            text_year = new Text(Resources.GetFont(Resources.FontResources.NinaB), year.ToString());
            Panel pan_day = new Panel();
            Panel pan_month = new Panel();
            Panel pan_year = new Panel();
            Panel pan_hour = new Panel();
            Panel pan_min = new Panel();

            pan_day.Children.Add(text_day);
            pan_month.Children.Add(text_month);
            pan_year.Children.Add(text_year);
            pan_hour.Children.Add(text_hour);
            pan_min.Children.Add(text_min);

            Image image_up_day = new Image(Resources.GetBitmap(Resources.BitmapResources.up));
            Image image_up_month = new Image(Resources.GetBitmap(Resources.BitmapResources.up));
            Image image_up_year = new Image(Resources.GetBitmap(Resources.BitmapResources.up));
            Image image_up_hour = new Image(Resources.GetBitmap(Resources.BitmapResources.up));
            Image image_up_min = new Image(Resources.GetBitmap(Resources.BitmapResources.up));

            Image image_down_day = new Image(Resources.GetBitmap(Resources.BitmapResources.down));
            Image image_down_month = new Image(Resources.GetBitmap(Resources.BitmapResources.down));
            Image image_down_year = new Image(Resources.GetBitmap(Resources.BitmapResources.down));
            Image image_down_hour = new Image(Resources.GetBitmap(Resources.BitmapResources.down));
            Image image_down_min = new Image(Resources.GetBitmap(Resources.BitmapResources.down));

            SimpleButton button_up_day = new SimpleButton(image_up_day, 20, 20);
            SimpleButton button_up_month = new SimpleButton(image_up_month, 20, 20);
            SimpleButton button_up_year = new SimpleButton(image_up_year, 20, 20);
            SimpleButton button_up_hour = new SimpleButton(image_up_hour, 20, 20);
            SimpleButton button_up_min = new SimpleButton(image_up_min, 20, 20);

            SimpleButton button_down_day = new SimpleButton(image_down_day, 20, 20);
            SimpleButton button_down_month = new SimpleButton(image_down_month, 20, 20);
            SimpleButton button_down_year = new SimpleButton(image_down_year, 20, 20);
            SimpleButton button_down_hour = new SimpleButton(image_down_hour, 20, 20);
            SimpleButton button_down_min = new SimpleButton(image_down_min, 20, 20);

            Canvas.SetTop(pan_day, 0);
            Canvas.SetLeft(pan_day, 0);
            canvas.Children.Add(pan_day);

            Canvas.SetTop(pan_month, 0);
            Canvas.SetLeft(pan_month, editDateWindow.Width / 4);
            canvas.Children.Add(pan_month);

            Canvas.SetTop(pan_year, 0);
            Canvas.SetLeft(pan_year, 2 * editDateWindow.Width / 4);
            canvas.Children.Add(pan_year);

            Canvas.SetTop(pan_hour, 0);
            Canvas.SetLeft(pan_hour, 3 * editDateWindow.Width / 4);
            canvas.Children.Add(pan_hour);

            Canvas.SetTop(pan_min, 0);
            Canvas.SetLeft(pan_min, 4 * editDateWindow.Width / 5+10);
            canvas.Children.Add(pan_min);

            Canvas.SetTop(button_up_day, 40);
            Canvas.SetLeft(button_up_day, 0);
            canvas.Children.Add(button_up_day);

            Canvas.SetTop(button_up_month, 40);
            Canvas.SetLeft(button_up_month, editDateWindow.Width / 4);
            canvas.Children.Add(button_up_month);

            Canvas.SetTop(button_up_year, 40);
            Canvas.SetLeft(button_up_year, editDateWindow.Width / 2);
            canvas.Children.Add(button_up_year);

            Canvas.SetTop(button_up_hour, 40);
            Canvas.SetLeft(button_up_hour, 3 * editDateWindow.Width / 4);
            canvas.Children.Add(button_up_hour);

            Canvas.SetTop(button_up_min, 40);
            Canvas.SetLeft(button_up_min, 4 * editDateWindow.Width / 5+10);
            canvas.Children.Add(button_up_min);

            Canvas.SetTop(button_down_day, 70);
            Canvas.SetLeft(button_down_day, 0);
            canvas.Children.Add(button_down_day);

            Canvas.SetTop(button_down_month, 70);
            Canvas.SetLeft(button_down_month, editDateWindow.Width / 4);
            canvas.Children.Add(button_down_month);

            Canvas.SetTop(button_down_year, 70);
            Canvas.SetLeft(button_down_year, editDateWindow.Width / 2);
            canvas.Children.Add(button_down_year);

            Canvas.SetTop(button_down_hour, 70);
            Canvas.SetLeft(button_down_hour, 3 * editDateWindow.Width / 4);
            canvas.Children.Add(button_down_hour);

            Canvas.SetTop(button_down_min, 70);
            Canvas.SetLeft(button_down_min, 4 * editDateWindow.Width / 5+10);
            canvas.Children.Add(button_down_min);

            Image image_accept = new Image(Resources.GetBitmap(Resources.BitmapResources.accept));
            image_accept.HorizontalAlignment = HorizontalAlignment.Center;
            image_accept.VerticalAlignment = VerticalAlignment.Center;
            SimpleButton button_accept = new SimpleButton(image_accept, editDateWindow.Width / 2, editDateWindow.Height / 5);
            button_accept.BackColor = Colors.White;
            Canvas.SetBottom(button_accept, 0);
            Canvas.SetLeft(button_accept, 0);
            canvas.Children.Add(button_accept);

            Image image_return = new Image(Resources.GetBitmap(Resources.BitmapResources.imreturn));
            image_return.HorizontalAlignment = HorizontalAlignment.Center;
            image_return.VerticalAlignment = VerticalAlignment.Center;
            SimpleButton button_return = new SimpleButton(image_return, editDateWindow.Width / 2, editDateWindow.Height / 5);
            button_return.BackColor = Colors.White;
            Canvas.SetBottom(button_return, 0);
            Canvas.SetRight(button_return, 0);
            canvas.Children.Add(button_return);

            Image image_internet = new Image(Resources.GetBitmap(Resources.BitmapResources.internet));
            image_internet.HorizontalAlignment = HorizontalAlignment.Center;
            image_internet.VerticalAlignment = VerticalAlignment.Center;
            SimpleButton button_internet = new SimpleButton(image_internet, 50, 50);
            button_internet.BackColor = Colors.White;
            Canvas.SetBottom(button_internet, 60);
            Canvas.SetLeft(button_internet, 0);
            canvas.Children.Add(button_internet);

            button_up_day.Click += new EventHandler(buttonUpDay_Click);
            button_up_month.Click += new EventHandler(buttonUpMonth_Click);
            button_up_year.Click += new EventHandler(buttonUpYear_Click);
            button_up_hour.Click += new EventHandler(buttonUpHour_Click);
            button_up_min.Click += new EventHandler(buttonUpMin_Click);

            button_down_day.Click += new EventHandler(buttonDownDay_Click);
            button_down_month.Click += new EventHandler(buttonDownMonth_Click);
            button_down_year.Click += new EventHandler(buttonDownYear_Click);
            button_down_hour.Click += new EventHandler(buttonDownHour_Click);
            button_down_min.Click += new EventHandler(buttonDownMin_Click);

            button_internet.Click += new EventHandler(buttonInternet_Click);
            button_accept.Click += new EventHandler(buttonAccept_Click);
            button_return.Click += new EventHandler(buttonReturn_Click);

            editDateWindow.Child = canvas;
            editDateWindow.Visibility = Visibility.Visible;
        }

        void buttonUpDay_Click(object sender, EventArgs evt)
        {
            day++;
            if (day == 32)
                day = 1;

            text_day.TextContent = day.ToString();
        }

        void buttonUpMonth_Click(object sender, EventArgs evt)
        {
            month++;
            if (month == 13)
                month = 1;

            text_month.TextContent = month.ToString();
        }

        void buttonUpYear_Click(object sender, EventArgs evt)
        {
            year++;

            text_year.TextContent = year.ToString();
        }

        void buttonUpHour_Click(object sender, EventArgs evt)
        {
            hour++;
            if (hour == 24)
                hour = 00;

            text_hour.TextContent = hour.ToString();
        }

        void buttonUpMin_Click(object sender, EventArgs evt)
        {
            min++;
            if (min == 60)
                min = 00;

            text_min.TextContent = min.ToString();
        }

        void buttonDownDay_Click(object sender, EventArgs evt)
        {
            day--;
            if (day == 0)
                day = 31;

            text_day.TextContent = day.ToString();
        }

        void buttonDownMonth_Click(object sender, EventArgs evt)
        {
            month--;
            if (month == 0)
                month = 12;

            text_month.TextContent = month.ToString();
        }

        void buttonDownYear_Click(object sender, EventArgs evt)
        {
            year--;

            text_year.TextContent = year.ToString();
        }

        void buttonDownHour_Click(object sender, EventArgs evt)
        {
            hour--;
            if (hour == -1)
                hour = 23;

            text_hour.TextContent = hour.ToString();
        }

        void buttonDownMin_Click(object sender, EventArgs evt)
        {
            min--;
            if (min == -1)
                min = 59;

            text_min.TextContent = min.ToString();
        }

        void buttonReturn_Click(object sender, EventArgs evt)
        {
            editDateWindow.Close();
        }

        void buttonAccept_Click(object sender, EventArgs evt)
        {            
            Utility.SetLocalTime(new DateTime(year, month, day, hour, min, 0));
            
            editDateWindow.Close();
            
        }

        void buttonInternet_Click(object sender, EventArgs evt)
        {
            Utility.SetLocalTime(GetNetworkTime());
            editDateWindow.Close();
        }
    
        public static DateTime GetNetworkTime()
        {
            IPEndPoint ep = new IPEndPoint(
                Dns.GetHostEntry("time-a.nist.gov").AddressList[0], 123);
            // time-a.nist.gov -- 129.6.15.28:123

            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            s.Connect(ep);

            byte[] ntpData = new byte[48]; // RFC 2030 
            ntpData[0] = 0x1B;
            for (int i = 1; i < 48; i++)
                ntpData[i] = 0;

            s.Send(ntpData);
            s.Receive(ntpData);
            s.Close();

            byte offsetTransmitTime = 40;
            ulong intpart = 0;
            ulong fractpart = 0;

            for (int i = 0; i <= 3; i++)
                intpart = 256 * intpart + ntpData[offsetTransmitTime + i];

            for (int i = 4; i <= 7; i++)
                fractpart = 256 * fractpart + ntpData[offsetTransmitTime + i];

            ulong milliseconds = (intpart * 1000 + (fractpart * 1000) / 0x100000000L);
            

            TimeSpan timeSpan = TimeSpan.FromTicks((long)milliseconds * TimeSpan.TicksPerMillisecond);

            DateTime dateTime = new DateTime(1900, 1, 1);
            dateTime += timeSpan;

            TimeSpan offsetAmount = TimeZone.CurrentTimeZone.GetUtcOffset(dateTime);
            DateTime networkDateTime = (dateTime + offsetAmount);

            return networkDateTime;
        }
    }
}
